<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateOrderRequest;
use App\Services\OrderService;
use App\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class OrderController extends Controller
{
    use ApiResponse;

    protected $orderService;

    public function __construct(OrderService $orderService)
    {
        $this->orderService = $orderService;
    }

    /**
     * 提交订单
     */
    public function store(CreateOrderRequest $request): JsonResponse
    {
        try {
            $user = Auth::user();
            $order = $this->orderService->createOrder($request->validated(), $user->id);

            return $this->created([
                'order' => $order
            ], '订单提交成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取订单详情
     */
    public function show(string $orderId): JsonResponse
    {
        try {
            $user = Auth::user();
            $order = $this->orderService->getOrderDetail($orderId, $user->id);

            return $this->success([
                'order' => $order
            ], '获取订单详情成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取我的订单
     */
    public function myOrders(): JsonResponse
    {
        try {
            $user = Auth::user();
            $orders = $this->orderService->getUserOrders($user->id);

            return $this->success([
                'orders' => $orders
            ], '获取订单列表成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取房间订单
     */
    public function getRoomOrders(Request $request, string $roomId): JsonResponse
    {
        try {
            $user = Auth::user();
            $orders = $this->orderService->getRoomOrders($roomId, $user->id);

            return $this->success([
                'orders' => $orders
            ], '获取房间订单成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 取消订单
     */
    public function destroy(string $orderId): JsonResponse
    {
        try {
            $user = Auth::user();
            $this->orderService->cancelOrder($orderId, $user->id);

            return $this->success([], '订单已取消');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 确认订单
     */
    public function confirm(Request $request, string $orderId): JsonResponse
    {
        try {
            $user = Auth::user();
            $this->orderService->confirmOrder($orderId, $user->id);

            return $this->success([], '订单已确认');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取订单统计
     */
    public function stats(Request $request, string $roomId): JsonResponse
    {
        try {
            $user = Auth::user();
            $stats = $this->orderService->getOrderStats($roomId);

            return $this->success([
                'stats' => $stats
            ], '获取订单统计成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }
}